package com.android.common.widget;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.core.util.Pair;
import androidx.fragment.app.FragmentManager;
import com.android.common.R;
import com.android.common.base.BaseDoSomeThing;
import com.android.common.utils.AppUtils;
import com.google.android.material.datepicker.CalendarConstraints;
import com.google.android.material.datepicker.DateValidatorPointForward;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.timepicker.MaterialTimePicker;
import com.google.android.material.timepicker.TimeFormat;


public class DialogFactory {

    private static AllDialog mLoadingAllDialog;

    private static AllDialog mPermissionAllDialog;

    /**
     * 加载dialog
     *
     * @param context
     * @return
     */
    public static AllDialog createLoading(Context context) {
        AllDialog.Builder builder = new AllDialog.Builder(context)
                .setLayoutId(R.layout.dialog_custom_loading)
                .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setCancelable(false)
                .setCanceledOnTouchOutside(false);
        mLoadingAllDialog = builder.build();
        return mLoadingAllDialog;
    }

    /**
     * 权限Dialog
     *
     * @param activity
     * @return
     */
    public static AllDialog createPermissionDialog(Activity activity) {
        if (mPermissionAllDialog == null) {
            AllDialog.Builder builder = new AllDialog.Builder(activity)
                    .setLayoutId(R.layout.dialog_common)
                    .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setListener((allDialog, view) -> {
                        TextView tvTitle = view.findViewById(R.id.title);
                        TextView tvContent = view.findViewById(R.id.content);
                        TextView tvCancel = view.findViewById(R.id.cancel);
                        TextView tvOk = view.findViewById(R.id.ok);
                        tvTitle.setText("提示");
                        tvContent.setText("请到设置中开启您拒绝的权限，不开启会导致某些功能不能正常使用");
                        tvCancel.setText("取消");
                        tvOk.setText("确定");
                        tvCancel.setOnClickListener(view1 -> {
                            allDialog.dismiss();
                        });

                        tvOk.setOnClickListener(view1 -> {
                            allDialog.dismiss();
                            AppUtils.openAppManagerSettings(activity);
                        });
                    })
                    .setCancelable(false)
                    .setCanceledOnTouchOutside(false);
            mPermissionAllDialog = builder.build();
        }
        return mPermissionAllDialog;
    }

    /**
     * 常用的弹窗，包含标题，提示语，取消按钮，功能按钮
     *
     * @param activity
     * @return
     */
    public static AllDialog createCommonDialog(Activity activity, String hintTitle, String hintMsg, String okText, BaseDoSomeThing<Void> doSomeThingOk) {
        AllDialog.Builder builder = new AllDialog.Builder(activity)
                .setLayoutId(R.layout.dialog_common)
                .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setListener((allDialog, view) -> {
                    TextView tvTitle = view.findViewById(R.id.title);
                    TextView tvContent = view.findViewById(R.id.content);
                    TextView tvCancel = view.findViewById(R.id.cancel);
                    TextView tvOk = view.findViewById(R.id.ok);
                    tvTitle.setText(hintTitle);
                    tvContent.setText(hintMsg);
                    tvCancel.setText(activity.getString(R.string.cancel));
                    tvOk.setText(okText);
                    tvCancel.setOnClickListener(view1 -> {
                        allDialog.dismiss();
                    });

                    tvOk.setOnClickListener(view1 -> {
                        doSomeThingOk.doSomeThing();
                        allDialog.dismiss();
                    });
                })
                .setCancelable(false)
                .setCanceledOnTouchOutside(false);
        return builder.build();
    }

    /**
     * 底部弹出选择图片对话框
     *
     * @param context
     * @return
     */
    public static AllDialog createChoosePic(Context context, BaseDoSomeThing<Void> takePhoto,
                                            BaseDoSomeThing<Void> choosePic, BaseDoSomeThing<Void> savePic) {
        AllDialog.Builder builder = new AllDialog.Builder(context)
                .setLayoutId(R.layout.dialog_choose_pic)
                .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                .setListener((allDialog, view) -> {
                    TextView tvTakePhoto = view.findViewById(R.id.tv_take_photo);
                    TextView tvChoosePic = view.findViewById(R.id.tv_choose_pic);
                    TextView tvSavePic = view.findViewById(R.id.tv_save_pic);
                    TextView tvCancel = view.findViewById(R.id.cancel);
                    //拍照
                    tvTakePhoto.setOnClickListener(v -> {
                        allDialog.dismiss();
                        takePhoto.doSomeThing();
                    });
                    //选择图片
                    tvChoosePic.setOnClickListener(v -> {
                        allDialog.dismiss();
                        choosePic.doSomeThing();
                    });
                    //保存图片
                    tvSavePic.setOnClickListener(v -> {
                        allDialog.dismiss();
                        savePic.doSomeThing();
                    });
                    //取消
                    tvCancel.setOnClickListener(v -> {
                        allDialog.dismiss();
                    });
                })
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        AllDialog dialog = builder.build();
        return dialog;
    }

    /**
     * 全屏日期范围选择
     *
     * @param manager
     * @param startTime
     * @param endTime
     * @param doSomeThing
     * @return
     */
    public static MaterialDatePicker<Pair<Long,Long>> showDateRangePicker(FragmentManager manager, long startTime, long endTime, BaseDoSomeThing doSomeThing) {
        return showDateRangePicker(manager,startTime,endTime,doSomeThing,null);
    }

    /**
     * 全屏日期范围选择
     *
     * @param manager
     * @param startTime
     * @param endTime
     * @return
     */
    public static MaterialDatePicker<Pair<Long,Long>> showDateRangePicker(FragmentManager manager, long startTime, long endTime, BaseDoSomeThing<Long> okDoSomeThing, BaseDoSomeThing<Void> dismissDoSomeThing) {
        MaterialDatePicker<Pair<Long,Long>> datePicker = MaterialDatePicker.Builder.dateRangePicker()
                .setSelection(new Pair<>(startTime, endTime))
                .setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
                .setTheme(R.style.MaterialCalendar_Fullscreen)
                .build();
        datePicker.addOnPositiveButtonClickListener(selection -> {
            Long startDate = selection.first;
            Long endDate = selection.second;
            if (null != okDoSomeThing){
                okDoSomeThing.doSomeThing(startDate, endDate);
            }
        });
        datePicker.addOnDismissListener(
            dialogInterface -> {
                if (null != dismissDoSomeThing){
                    dismissDoSomeThing.doSomeThing();
                }
            });
        datePicker.show(manager, "dateRangePicker");
        return datePicker;
    }

    /**
     * 全屏选择日期
     *
     * @param manager
     * @return
     */
    public static MaterialDatePicker<Long> showSingleDatePicker(FragmentManager manager, long selectTime,BaseDoSomeThing<Long> okDoSomeThing) {
        CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
        constraintsBuilder.setValidator(DateValidatorPointForward.now());
        MaterialDatePicker<Long> datePicker = MaterialDatePicker.Builder.datePicker()
                .setSelection(selectTime)
                .setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
                .setTheme(R.style.MaterialCalendar_Fullscreen)
                .setCalendarConstraints(constraintsBuilder.build())
                .build();
        datePicker.addOnPositiveButtonClickListener(selection -> {
            if (null != okDoSomeThing){
                okDoSomeThing.doSomeThing(selection);
            }
        });
        datePicker.show(manager, "singleDatePicker");
        return datePicker;
    }

    /**
     * 选择时间
     *
     * @param manager
     * @return
     */
    public static MaterialTimePicker showTimePicker(FragmentManager manager,int hour,int minute,BaseDoSomeThing<Integer> okDoSomeThing) {
        MaterialTimePicker timePicker = new MaterialTimePicker.Builder()
                .setTheme(R.style.MaterialTimePicker)
                .setInputMode(MaterialTimePicker.INPUT_MODE_CLOCK)
                .setTimeFormat(TimeFormat.CLOCK_24H)
                .setTitleText("请选择时间")
                .setHour(hour)
                .setMinute(minute)
                .build();
        timePicker.addOnPositiveButtonClickListener( view->{
            okDoSomeThing.doSomeThing(timePicker.getHour(),timePicker.getMinute());
        });
        timePicker.show(manager, "timePicker");
        return timePicker;
    }

}
